package com.linkage.usercenter.controller;

import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.linkage.common.base.RequestInfo;
import com.linkage.common.base.ResponseInfo;
import com.linkage.common.constant.CommonSysConstant;
import com.linkage.usercenter.pojo.baseDO.PoiBAExcelInfoVO;
import com.linkage.usercenter.pojo.entity.CoreUser;
import com.linkage.usercenter.pojo.entity.MgrLoginDaily;
import com.linkage.usercenter.pojo.vo.*;
import com.linkage.usercenter.pojo.vo.managevo.MgDptInfosLineVO;
import com.linkage.usercenter.service.*;
import com.linkage.usercenter.utils.ExcelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/exter")
public class PoiExcelController {

    private static final Logger logger = LoggerFactory.getLogger(PoiExcelController.class);


    @Autowired
    private IManageService manageService;
    @Autowired
    private IuserToolService userToolService;

    @Autowired
    private IuserService userService;

    @Autowired
    private IDepService depService;

    @Autowired
    private MgrLoginService mgrLoginService;


    //下载文件测试用
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    public void exportExcel() throws IOException {
        List<PoiBAExcelInfoVO> resultList = new ArrayList<PoiBAExcelInfoVO>();
        PoiBAExcelInfoVO busClick = new PoiBAExcelInfoVO();
        busClick.setUserCode("sss");
        busClick.setUserExpireTimeLongDay("88");
        busClick.setUserMobile("13300005555");
        resultList.add(busClick);

        PoiBAExcelInfoVO busClick1 = new PoiBAExcelInfoVO();
        busClick1.setUserCode("sss6");
        busClick1.setUserExpireTimeLongDay("886");
        busClick1.setUserMobile("13300005556");
        resultList.add(busClick1);
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//            HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        long t1 = System.currentTimeMillis();
        String fileName = "测试文件.xlsx";
        File file1 = new File("/ssss.txt");
        System.out.println(file1.getName()+"----"+ file1.getAbsolutePath());
        System.out.println(this.getClass()+this.getClass().getResource("./").getPath());
        System.out.println(this.getClass()+this.getClass().getResource("/").getPath());
        logger.info("生成文件开始："+fileName);

        ExcelUtils.writeExcel(response, resultList, PoiBAExcelInfoVO.class, fileName);
        long t2 = System.currentTimeMillis();
        logger.info(String.format("解析文档完成! 耗时:%sms", (t2 - t1)));
    }

    //上传文件测试
    @RequestMapping(value = "/readExcel", method = RequestMethod.POST)
    public String  readExcel(@RequestParam(value = "uploadFile", required = false) MultipartFile file,
                             @RequestParam(value = "fileType", required = false) String fileType) {
        long t1 = System.currentTimeMillis();
        logger.info("上传文件开始："+file.getOriginalFilename());
        List<PoiBAExcelInfoVO> list = ExcelUtils.readExcel("", PoiBAExcelInfoVO.class, file);
        long t2 = System.currentTimeMillis();
        logger.info(String.format("解析文档完成! 耗时:%sms", (t2 - t1)));
        list.forEach(
                b -> {
                    System.out.println(JSON.toJSONString(b));
                }
        );
        return  ResponseInfo.success(list).toString();
    }



    //上传文件测试
    @RequestMapping(value = "/upload/userExcel", method = RequestMethod.POST)
    public String  userExcel(@RequestParam(value = "uploadFile", required = false) MultipartFile file ) {
        long t1 = System.currentTimeMillis();
        List<Map> list = new ArrayList<Map>();
        logger.info("上传文件开始："+file.getOriginalFilename());
        List<PoiBAExcelInfoVO> requestList = ExcelUtils.readExcel("", PoiBAExcelInfoVO.class, file);
        long t2 = System.currentTimeMillis();
        logger.info(String.format("解析文档完成! 耗时:%sms", (t2 - t1)));
        requestList.forEach(
                b -> {
                    logger.info(JSON.toJSONString(b));
                }
        );
        userService.userExcel(requestList, list);
        return  ResponseInfo.success(list).toString();
    }


}
